package com.aplink.generic.database;

import com.aplink.generic.constant.type.SqlDataType;
import com.aplink.generic.pojo.ObjPropInfo;

public class SqlMethods {

    private static final String AND = " AND ";
    private static final String EQUAL = " = ";
    private static final String LIKE = " LIKE ";
    private static final String OR = " OR ";

    public static SqlMethods getInstance() {
        return new SqlMethods();
    }

    private final StringBuilder mWhereClause;

    private SqlMethods() {
        this.mWhereClause = new StringBuilder();
    }

    public SqlMethods and() {
        this.mWhereClause.append(SqlMethods.AND);
        return this;
    }

    public SqlMethods and(final String clause) {
        this.mWhereClause.append(SqlMethods.AND).append(clause);
        return this;
    }

    public SqlMethods equal(final ObjPropInfo column, final String value) {
        if (column.isUsername()) {
            this.mWhereClause.append(String.format("UPPER(%s)",
                    column.getFullColumnName()));
            this.mWhereClause.append(SqlMethods.EQUAL);
            this.mWhereClause.append(String.format("UPPER('%s')", value));
            return this;
        }
        final String columnName = column.getFullColumnName();
        return this.equal(columnName, value);
    }

    public SqlMethods equal(final String column, final String value) {
        this.mWhereClause.append(String.format("%s", column));
        this.mWhereClause.append(SqlMethods.EQUAL);
        this.mWhereClause.append(String.format("'%s'", value));
        return this;
    }

    public SqlMethods equalPassword(final SqlDataType sqlDataType,
            final String column, final String value) {
        if (sqlDataType.equals(SqlDataType.CHAR)) {
            this.mWhereClause.append(String.format("%s='%s'", column, value));
        } else if (sqlDataType.equals(SqlDataType.INTEGER)
                || sqlDataType.equals(SqlDataType.DOUBLE)
                || sqlDataType.equals(SqlDataType.LONG)) {
            this.mWhereClause.append(column);
            this.mWhereClause.append(SqlMethods.EQUAL);
            this.mWhereClause.append(value);
        }
        return this;
    }

    public SqlMethods isNotNull(final String column) {
        this.mWhereClause.append(column).append(" is not null ");
        return this;
    }

    public SqlMethods isNull(final String column) {
        this.mWhereClause.append(column).append(" is null ");
        return this;
    }

    public SqlMethods like(final String column, final String value) {
        this.mWhereClause.append(column);
        this.mWhereClause.append(SqlMethods.LIKE);
        this.mWhereClause.append(value);
        return this;
    }

    public SqlMethods or() {
        this.mWhereClause.append(SqlMethods.OR);
        return this;
    }

    public SqlMethods or(final String clause) {
        this.mWhereClause.append(SqlMethods.OR).append(clause);
        return this;
    }

    @Override
    public String toString() {
        if (this.mWhereClause.length() == 0) {
            return null;
        }
        return this.mWhereClause.toString();
    }
}
